package org.com.yz.dubbo.web.ctrl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringEscapeUtils;
import org.com.yz.common.annotion.Parameter;
import org.com.yz.common.constant.PayConstant;
import org.com.yz.common.util.ClassRefUtil;
import org.com.yz.common.util.MyLog;
import org.com.yz.common.util.ToolUtil;
import org.com.yz.dal.dao.model.MchAccnt;
import org.com.yz.dal.dao.model.MchSubInfo;
import org.com.yz.dubbo.api.service.IMchAccntService;
import org.com.yz.dubbo.web.service.RpcCommonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * 创建子商户账户控制类
 *
 * @author
 * @date
 */
@RestController
public class CreateMchSubController {

    private final MyLog _log = MyLog.getLog(CreateMchSubController.class);

    @Reference(version = "1.0.0", timeout = 10000, retries = 0)
    public IMchAccntService iMchAccntService;

    @Autowired
    private RpcCommonService rpcCommonService;

    @PostMapping(value = "/api/createMchSubMchAccnt.htm")
    @Parameter
    public String createMchSubMchAccnt(String data, String sign) {
        String result = StringEscapeUtils.unescapeJava(data);
        MchAccnt mchAccnt = null;
        MchSubInfo mchReq = JSONObject.parseObject(result, MchSubInfo.class);//解析data为对象
        MchSubInfo mchRes = new MchSubInfo();//返回的对象
        String mchNo = mchReq.getMchNo();//商户号
        //外部子商户号已存在，返回失败，但是返回信息里加上子商户账号
        mchAccnt = new MchAccnt();
        mchAccnt.setOutMchAccntNo(mchReq.getBizContent().getOutMchAccntNo());
        mchAccnt.setMchNo(mchNo);
        //根据外部子商户号跟商户号查询子商户信息
        List<MchAccnt> mchAccntList = iMchAccntService.selectMchAccnt(mchAccnt);
        if (mchAccntList.size() > 0) {
            mchAccnt = new MchAccnt();
            mchAccnt.setMchAccntNo(mchAccntList.get(0).getMchAccntNo());
            mchAccnt.setOutMchAccntNo(mchAccntList.get(0).getOutMchAccntNo());
            mchRes.setBizContent(mchAccnt);
            return ClassRefUtil.createRetData(mchReq, mchRes, PayConstant.RETURN_DEPOSIT_MCHNO_REPEAT, PayConstant.RETURN_DEPOSIT_MCHNO_REPEATS);
        }
        try {
            //创建子商户账户
            MchAccnt results = rpcCommonService.createMchSubService.createMchSub(data);
            //如果创建失败
            if (ToolUtil.isEmpty(results)) {
                mchRes.setBizContent(null);
                _log.info("创建子商户账户失败");
                return ClassRefUtil.createRetData(mchReq, mchRes, PayConstant.RETURN_DEPOSIT_CODE_ERROR, PayConstant.RETURN_DEPOSIT_CODES_ERROR);
            }
            mchRes.setBizContent(results);//将子商户信息返回
            return ClassRefUtil.createRetData(mchReq, mchRes, PayConstant.RETURN_DEPOSIT_SUCESS, PayConstant.RETURN_ALIPAY_VALUE_SUCCESS);
        } catch (Exception e) {
            _log.error(e, ToolUtil.getExceptionMsg(e));
            return ClassRefUtil.createRetData(mchReq, mchRes, PayConstant.RETURN_DEPOSIT_CODE_ERROR, PayConstant.RETURN_DEPOSIT_CODES_ERROR);
        }
    }


    /**
     * 回调测试
     * @param data
     * @param sign
     * @return
     */
    @PostMapping(value = "/api/te.htm")
    public String createMchSubMchAccnt1(String data, String sign) {
        JSONObject po = JSONObject.parseObject(data);
        System.out.println(po.getString("amount"));
        return "success";
    }



}
